package security

import (
	"go_gvm_mini/instructions/base"
	"go_gvm_mini/native"
	"go_gvm_mini/rtda"
)

func init() {
	native.Registers("java/security/AccessController", []*native.NaMethod{
		{
			"doPrivileged",
			"(Ljava/security/PrivilegedAction;)Ljava/lang/Object;",
			doPrivileged,
		}, {
			"doPrivileged",
			"(Ljava/security/PrivilegedExceptionAction;)Ljava/lang/Object;",
			doPrivileged,
		}, {
			"getStackAccessControlContext",
			"()Ljava/security/AccessControlContext;",
			getStackAccessControlContext,
		},
	})
}

// @CallerSensitive
// public static native <T> T
//     doPrivileged(PrivilegedExceptionAction<T> action)
//     throws PrivilegedActionException;
// (Ljava/security/PrivilegedExceptionAction;)Ljava/lang/Object;

// @CallerSensitive
// public static native <T> T doPrivileged(PrivilegedAction<T> action);
// (Ljava/security/PrivilegedAction;)Ljava/lang/Object;
func doPrivileged(jFrame *rtda.JFrame) {
	vars := jFrame.LocalVars()
	action := vars.GetRef(0)

	stack := jFrame.OperandStack()
	stack.PushRef(action)

	method := action.Class().GetInstanceMethod("run", "()Ljava/lang/Object;") // todo
	base.InvokeMethod(jFrame, method)
}

// private static native AccessControlContext getStackAccessControlContext();
// ()Ljava/security/AccessControlContext;
func getStackAccessControlContext(jFrame *rtda.JFrame) {
	// todo
	jFrame.OperandStack().PushRef(nil)
}
